//
//  WriteStateViewController.m
//  uniquelove
//
//  Created by ibokan on 14-12-16.
//  Copyright (c) 2014年 ibokan. All rights reserved.
//

#import "WriteStateViewController.h"
#import "AFNetworking.h"
#import "GetStateViewController.h"


@interface WriteStateViewController ()
@property (strong, nonatomic) IBOutlet UITextView *textView;
- (IBAction)buttonPressed:(id)sender;

@end

@implementation WriteStateViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.textView.delegate = self;
}



- (IBAction)buttonPressed:(id)sender {
    //发送状态
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    NSString *token = (NSString *)[[NSUserDefaults standardUserDefaults]objectForKey:@"accessToken"];
    NSString *userID = (NSString *)[[NSUserDefaults standardUserDefaults]objectForKey:@"userID"];
    NSDictionary *dic = @{@"access_token": token,@"uid":userID,@"content":self.textView.text};
   [manager POST:@"http://lovemyqq.sinaapp.com/postState.php" parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
       NSLog(@"%@",responseObject);
       [self.navigationController popViewControllerAnimated:YES];
    [self.getState refreshTheData];
   } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
       NSLog(@"%@",error);
   }];
    self.textView.text = nil;
    
}

//动态改变textView的尺寸
-(void)textViewDidChange:(UITextView *)textView{
    float fPadding = 16.0; // 8.0px x 2
    UIFont *font = self.textView.font;
    NSDictionary *attrs = @{NSFontAttributeName : font};
    CGSize constraint = CGSizeMake(self.textView.contentSize.width - fPadding, CGFLOAT_MAX);
    CGSize size = [self.textView.text boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
    self.textView.frame = CGRectMake(self.textView.frame.origin.x, self.textView.frame.origin.y, self.textView.frame.size.width    , size.height + 16.0);
}

@end
